// pages/clerkSubOrder/clerkSubOrder.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fphone: '', //手机号
    fpicktype: '送货', //提货方式
    fpickadr: '', //提货地址
    ffetchpic: [], //取货图片
    ffetchtext: '', //备注
    index1: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    index5: 0,
    index6: 0,
    index11: 0,
    shopName: [], //店名
    baseImgPath: app.globalData.baseImgPath,
    backImgPath: app.globalData.URL,
    ftype: [], //服务类别
    fser: [], //服务内容
    fbrand: [], //品牌
    fmaterial: ['光面、布面类', '麂皮、磨砂、珠光', '漆皮'], //材质
    fsize: ['大', '中', '小'], //大小
    imgList: [], //上传图片
    imgList1: [], //上传图片
    address: '南开区', //送货地址
    sessionId: 0, //sessionId
    fcid: '', //服务类别id
    cid:'',
    items: [{
        name: '送货',
        value: '送货',
        checked: 'true'
      },
      {
        name: '自提',
        value: '自提'
      },
    ],
    fexname: ['顺丰'],
    selected: true,
    selected1: false,
    pics: [],
    picss: [],
    pic: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this;
    wx.getStorage({
      key: 'sessionId',
      success: function(res) {
        that.setData({
          sessionId: res.data, //登陆页面取回来的sessionId
        });
        console.log(that.data.sessionId);
        //服务内容类型和内容查询
        wx.request({
          url: app.globalData._urlPath + 'BespeakServe/cateList',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          data: {
            sessionid: that.data.sessionId
          },
          method: "POST",
          success: function(res) {
            console.log(res)
            if (res.data.code == 200) {
              that.setData({
                ftype: res.data.data,
              })
              //根据传的服务类别查询对应的服务内容
              wx.request({
                url: app.globalData._urlPath + 'BespeakServe/cateList',
                header: {
                  'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: {
                  sessionid: that.data.sessionId,
                  id: that.data.ftype[0].fcid
                },
                method: "POST",
                success: function(res) {
                  console.log(res)
                  if (res.data.code == 200) {
                    that.setData({
                      fser: res.data.data
                    })
                  }
                  if (res.data.code == 400) {
                    that.setData({
                      fser: []
                    })
                    wx.showToast({
                      title: res.data.error,
                      icon: 'none',
                      duration: 2000,
                      mask: true
                    })
                  }
                }
              });
            }
            if (res.data.code == 400) {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000,
                mask: true
              })
            }
          }
        });
        //查询品牌
        wx.request({
          url: app.globalData._urlPath + 'pindex/get_brand',
          data: {
            cid: 2
          },
          method: "get",
          success: function(res) {
            console.log(res)
            if (res.data.code == 200) {
              that.setData({
                fbrand: res.data.data
              })
            }
            if (res.data.code == 400) {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000,
                mask: true
              })
            }
          }
        });
      },
    })
    //获取店名
    wx.request({
      url: app.globalData._urlPath + 'pindex/stores',
      method: 'POST',
      data: {},
      success: function(res) {
        let list = res.data.data;
        let newList = [];
        list.forEach(function(item) {
          newList.push(item.st_name)
        })
        that.setData({
          shopName: newList
        })
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindchange1: function(e) {
    var that = this;
    var index = e.detail.value;
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    that.setData({
      index1: index
    })
    var ficd = this.data.ftype[this.data.index1].fcid;
    that.setData({
      fcid: ficd,
      cid: ficd
    })

    //根据传的服务类别查询对应的服务内容
    wx.request({
      url: app.globalData._urlPath + 'BespeakServe/cateList',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        sessionid: that.data.sessionId,
        id: that.data.fcid
      },
      method: "POST",
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            fser: res.data.data
          })
        }
        if (res.data.code == 400) {
          that.setData({
            fser: []
          })
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      }
    });
    //查询品牌
    wx.request({
      url: app.globalData._urlPath + 'pindex/get_brand',
      data: {
        cid: that.data.cid
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: "get",
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            fbrand: res.data.data
          })
        }
        if (res.data.code == 400) {
          that.setData({
            fbrand: [{'bname':'其他','bid':0}]
          })
        }
      }
    });
  },
  bindchange2: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index2: index
    })
    console.log(that.data.fser[that.data.index2].fname)
  },
  bindchange3: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index3: index
    })
    console.log(that.data.fbrand[that.data.index3].bname)
  },
  bindchange4: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index4: index
    })
    console.log(that.data.fmaterial[that.data.index4])
  },
  radioChange: function(e) {
    let way = e.detail.value;
    //console.log('radio发生change事件，携带value值为：', e.detail.value);
    this.setData({
      fpicktype: e.detail.value
    })
  },
  bindchange5: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index5: index
    })
    console.log(that.data.fsize[that.data.index5])
  },
  bindchange6: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index6: index
    })

  },
  bindchange11: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index11: index
    })
  },
  //提交表单
  formSubmit: function(e) {
    var that = this;
    var address = "";
    if (this.data.fpicktype == '送货') {
      address = e.detail.value.fpickadr;
    } else {
      console.log(this.data.shopName)
      address = this.data.shopName[this.data.index6]
    }
    console.log(address)
    let data = JSON.stringify({
      fphone: e.detail.value.phone,
      ftype: that.data.ftype[that.data.index1].fname,
      fser: that.data.fser[that.data.index2].fname,
      fbrand: that.data.fbrand[that.data.index3].bname,
      fmaterial: that.data.fmaterial[that.data.index4],
      fsize: that.data.fsize[that.data.index5],
      fpicktype: that.data.fpicktype,
      fpickadr: address,
      ffetchadr: address,
      uname: e.detail.value.name,
      ffetchpic: app.globalData.pic,
      ffetchtext: e.detail.value.textarea,
      sessionid: that.data.sessionId
    })
    console.log("准备提交的" + data)
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/clerkSub',
      data: data,
      method: "POST",
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.data,
            icon: 'success',
            duration: 2000,
            mask: true
          });
          app.globalData.pic = [];
          setTimeout(function() {
            wx.redirectTo({
              url: '/pages/clearkOrderList/clearkOrderList'
            })
          }, 2000)
        }
        if (res.data.code == 400) {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      }
    })
  },
  formSubmit1: function(e) {
    var that = this;
    let data = JSON.stringify({
      fexname: 'shunfeng',
      fexcode: e.detail.value.yundan,
      ffetchpic: app.globalData.pic1,
      ffetchtext: e.detail.value.textarea1,
      sessionid: that.data.sessionId
    })
    console.log("准备提交的" + data)
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/ffetchex',
      data: data,
      method: "POST",
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.data,
            icon: 'success',
            duration: 2000,
            mask: true
          });
          app.globalData.pic1 = [];
          setTimeout(function() {
            wx.redirectTo({
              url: '/pages/clearkOrderList/clearkOrderList?current=1'
            })
          }, 2000)
        }
        if (res.data.code == 400) {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      }
    })
  },
  //底部导航切换
  handleChangeTab({
    detail
  }) {
    var that = this;
    console.log(detail.key)
    if (detail.key == 'order') {
      wx.redirectTo({
        url: '/pages/clerkSubOrder/clerkSubOrder',
      })
    } else if (detail.key == 'code') {
      // 只允许从相机扫码
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          console.log(res);
          that.setData({
            qrcode: res.result
          })
          wx.showModal({
            title: '是否确认收货?',
            // content: '模态弹窗',
            success: function(res) {
              if (res.confirm) {
                //加载二维码返回数据
                wx.request({
                  url: app.globalData.urlPath + '/ManagerMent/scanqrcode',
                  data: {
                    sessionid: app.globalData.sessionid,
                    qrcode: that.data.qrcode
                  },
                  method: "POST",
                  success: function(res) {
                    console.log(res)
                    if (res.data.code == 200) {
                      wx.showToast({
                        title: res.data.data,
                        icon: 'success',
                        duration: 2000
                      })
                      setTimeout(function() {
                        wx.redirectTo({
                          url: '/pages/clearkOrderList/clearkOrderList?current=5',
                        })
                      })
                    } else {
                      wx.showToast({
                        title: res.data.error,
                        icon: 'none',
                        duration: 2000
                      })
                      if (res.data.code == 5000) {
                        wx.redirectTo({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  }
                })
              } else {
                return;
              }

            }
          })
        }
      })
    } else if (detail.key == 'my') {
      wx.redirectTo({
        url: '/pages/clerkMy/clerkMy',
      })
    }
    this.setData({
      currentTab: detail.key
    });
  },
  choose: function() { //这里是选取图片的方法
    var that = this;
    var pics = [];
    wx.chooseImage({
      count: 9 - pics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res) {
        console.log(res);
        var imgsrc = res.tempFilePaths;
        pics = that.data.imgList.concat(imgsrc);
        that.setData({
          pics: pics
        });

        that.uploadimg(imgsrc);
        //if (app.globalData.successN == imgsrc.length){
        // that.setData({
        //   imgList: app.globalData.pic
        // })
        that.setData({
          imgList: pics
        })
        console.log(that.data.pics)
        //}
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })

  },
  choose1: function () { //这里是选取图片的方法
    var that = this;
    var picss = [];
    wx.chooseImage({
      count: 9 - picss.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        console.log(res);
        var imgsrc1 = res.tempFilePaths;
        picss = that.data.imgList1.concat(imgsrc1);
        that.setData({
          picss: picss
        });

        that.uploadimg2(imgsrc1);
        //if (app.globalData.successN == imgsrc.length){
        // that.setData({
        //   imgList: app.globalData.pic
        // })
        that.setData({
          imgList1: picss
        })
        console.log(that.data.picss)
        //}
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },
  uploadimg: function(imgsrc) { //这里触发图片上传的方法
    var that = this;
    var pics = this.data.pics;
    console.log(pics)
    app.uploadimg1({
      url: app.globalData.urlPath + 'ManagerMent/uploadpic', //这里是你图片上传的接口
      path: imgsrc //这里是选取的图片的地址数组
    });
  },
  uploadimg2: function (imgsrc1) { //这里触发图片上传的方法
    var that = this;
    var picss = this.data.picss;
    console.log(picss)
    app.uploadimg2({
      url: app.globalData.urlPath + 'ManagerMent/uploadpic', //这里是你图片上传的接口
      path: imgsrc1 //这里是选取的图片的地址数组
    });
  },
  //删除上传的图片
  deletePic: function(e) {
    console.log(e)
    var self = this;
    wx.showToast({
      title: '删除成功！',
    })
    var imgs = self.data.imgList;
    var pics=app.globalData.pic;
    //console.log(pics)
    console.log(app.globalData.pic)
    var index = e.currentTarget.dataset.index;
    console.log(index)
    pics.splice(index, 1);
    imgs.splice(index, 1);
 
    self.setData({
      imgList: imgs
    });
    console.log(self.data.imgList)
    console.log(app.globalData.pic)
    
    wx.setStorage({
      key: 'imgPic',
      data: self.data.imgList,
    })
    // app.globalData.pic = self.data.imgList;
  },
  deletePic1: function (e) {
    var self = this;
    wx.showToast({
      title: '删除成功！',
    })
    var imgs = self.data.imgList1;
    var pics = app.globalData.pic1;
    //console.log(pics)
    console.log(app.globalData.pic1)
    var index = e.currentTarget.dataset.index;
    console.log(index)
    pics.splice(index, 1);
    imgs.splice(index, 1);

    self.setData({
      imgList1: imgs
    });
    console.log(self.data.imgList1)
    console.log(app.globalData.pic1)

    wx.setStorage({
      key: 'imgPic1',
      data: self.data.imgList1,
    })
    // app.globalData.pic = self.data.imgList;
  },
  //点击我服务的订单
  linkToOrderList: function() {
    wx.redirectTo({
      url: '/pages/clearkOrderList/clearkOrderList'
    })
  },
  /**
   * 滑动切换tab
   */
  bindChange: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  selected: function(e) {
    this.setData({
      selected1: false,
      selected: true
    })
  },
  selected1: function(e) {
    this.setData({
      selected: false,
      selected1: true
    })
  }
})